package cc.comac.mom.controller;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cc.comac.mom.api.QualityAttachmentService;
import cc.comac.mom.config.OSSService;
import cc.comac.mom.entity.QualityAttachmentEntity;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.util.UploadPreviewData;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/QualityAttachment")
public class QualityAttachmentController {
	@Autowired
	private QualityAttachmentService qualityAttachmentService;

	@Autowired
	private OSSService oSSService;
	/**
	 * 文件上传
	 * @param files
	 * @return
	 */
	@ApiOperation("文件上传批量")
	@RequestMapping(value = "/upload",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public ResultBean upload (@RequestPart("files") MultipartFile[] files) {
		return qualityAttachmentService.uploadFile(files);
	}

	@ApiOperation("图片预览功能")
	@RequestMapping(value = "/previewFile",method = RequestMethod.GET)
	public ResultBean previewPdf( HttpServletResponse response, @RequestParam(value = "id") Long attachmentId) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (attachmentId == 0) {
			resultBean.setErrorMessge("无法获取附件主键");
			return resultBean;
		}
		QualityAttachmentEntity byAttachmentId = qualityAttachmentService.getByAttachmentId(attachmentId);
		//QualityAttachmentEntity innerData = (QualityAttachmentEntity)byAttachmentId.getObject();
		String checkPicturePath = byAttachmentId.getFilePath();
		// 图片预览
		UploadPreviewData.previewPdf(checkPicturePath, response);
		return resultBean;
	}

	@ApiOperation("文件上传批量，不分组，文件信息返回在resultBean中")
	@RequestMapping(value = "/uploadFileNoGroup",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public ResultBean uploadFileNoGroup (@RequestPart("files") MultipartFile[] files) {
		return qualityAttachmentService.uploadFileNoGroup(files);
	}
	/**
	 * 删除附件通过附件id
	 */
	@ApiOperation("删除附件通过附件id")
	@RequestMapping(value = "/deleteFileByAttachmentId/{attachmentId}",method = RequestMethod.GET)
	public ResultBean deleteFileByAttachmentId (@PathVariable long attachmentId) {
		return qualityAttachmentService.deleteFileByAttachmentId(attachmentId);
	}
	
	/**
	 * 通过附件组id获取附件信息
	 */
	@ApiOperation("通过附件组id获取文件信息")
	@RequestMapping(value = "/getByAttachmentGroupId/{groupId}",method = RequestMethod.GET)
	public ResultBean getByAttachmentGroupId (@PathVariable long groupId) {
		return qualityAttachmentService.getByAttachmentGroupId(groupId);
	}
	
	@GetMapping("/download/{attachmentId}")
	public ResultBean download (@PathVariable("attachmentId") Long attachmentId, HttpServletResponse request,  HttpServletResponse response) {
		QualityAttachmentEntity byAttachmentId = qualityAttachmentService.getByAttachmentId(attachmentId);
		if (byAttachmentId!=null) {
//			UploadPreviewData.download(byAttachmentId.getFilePath(), byAttachmentId.getRealName(), response);
			try {
				oSSService.downFileFromOSS(response, byAttachmentId.getFilePath(), byAttachmentId.getRealName());
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		
		}
		return null;
	}

	@GetMapping(value = "/previewImage/{documentId}")
	public ResponseEntity<Object> previewImage(@PathVariable("documentId") Long documentId, HttpServletResponse response, HttpServletRequest request)  {
		QualityAttachmentEntity byAttachmentId = qualityAttachmentService.getByAttachmentId(documentId);
		try {
			// 以流的形式下载文件。
			InputStream fis=oSSService.getFileByPathFromOss(byAttachmentId.getFilePath());

			ByteArrayResource bar = new ByteArrayResource(IOUtils.toByteArray(fis));

			return ResponseEntity.ok()
					.header("Content-Disposition", "inline;filename=" +byAttachmentId.getRealName() + "")
					.contentType(MediaType.IMAGE_PNG)
					.contentLength(bar.contentLength())
					.body(bar);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@GetMapping(value = "/previewPdf/{documentId}")
	public ResponseEntity<Object> previewPdf(@PathVariable("documentId") Long documentId, HttpServletResponse response, HttpServletRequest request)  {
		QualityAttachmentEntity byAttachmentId = qualityAttachmentService.getByAttachmentId(documentId);
		try {
			// 以流的形式下载文件。
			InputStream fis=oSSService.getFileByPathFromOss(byAttachmentId.getFilePath());

			ByteArrayResource bar = new ByteArrayResource(IOUtils.toByteArray(fis));

			return ResponseEntity.ok()
					.header("Content-Disposition", "inline;filename=" +byAttachmentId.getRealName() + "")
					.contentType(MediaType.APPLICATION_PDF)
					.contentLength(bar.contentLength())
					.body(bar);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
